home *** CD-ROM | disk | FTP | other *** search
/ Freelog 9 / Freelog009.iso / BAS / Internet / Rtf2Html / Source C / LIB / LIB.C < prev    next >
C/C++ Source or Header  |  1999-06-27  |  8KB  |  463 lines

  1.  
  2. /*
  3.  * %%File: lib.c
  4.  *
  5.  * Chaque nom de fonction definie dans ce fichier est precede 
  6.  * des lettres : lib
  7.  *
  8.  * Copyright (c) Bertrand LE QUELLEC 1995-1999
  9.  *
  10.  * http://perso.wanadoo.fr/blq
  11.  * blq@wanadoo.fr
  12.  */
  13.  
  14.  
  15. #include <stdio.h>
  16.  
  17. #ifdef IRIX
  18. #include <string.h>
  19. #include <stddef.h>
  20. #include <ctype.h>
  21. #endif
  22.  
  23. #ifndef UNIX_SRC   
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <malloc.h>
  27. #endif
  28.  
  29. #define SOURCE_LIB     1
  30. #include "lib.h"
  31.  
  32.  
  33.  
  34. static char libTmpTab[MLEN * 4];
  35. static int  oldChar;
  36.  
  37.  
  38. /*
  39.  * %%Function: libPairImpair
  40.  *
  41.  * Renvoir True si chiffre pair ou False si impair.
  42.  */
  43. Bool libPairImpair(int n)
  44. {               
  45.     char tabTmp [512];
  46.     
  47.     
  48.     if (!n) return True;
  49.     
  50.     sprintf(tabTmp, "%d", n);
  51.     
  52.     switch(tabTmp[strlen(tabTmp) - 1])
  53.     {
  54.         case 1:
  55.         case 3:
  56.         case 5:
  57.         case 7: 
  58.         case 9 :
  59.             return False;
  60.         
  61.         case 0:
  62.         case 2:
  63.         case 4:
  64.         case 6:
  65.         case 8 :
  66.             return True;
  67.         
  68.         default :
  69.             return False;
  70.     }
  71. }
  72.  
  73. /*
  74.  * %%Function: libBitCount
  75.  *
  76.  * Inverse une chaine.
  77.  */
  78. int libBitCount(unsigned n)
  79. {
  80.     int b;
  81.     
  82.     for(b = 0; n != 0; n >>= 1)
  83.     {        
  84.         if(n & 01)
  85.             b++;
  86.     }
  87.     
  88.     return b;  
  89. }
  90.  
  91. /*
  92.  * %%Function: libReverse
  93.  *
  94.  * Inverse une chaine.
  95.  */
  96. void libReverse(char chaine[])
  97. {  
  98.     int c, i, j;
  99.     
  100.     for (i = 0, j = strlen(chaine) - 1; i < j; i++, j--)
  101.     {
  102.         c = chaine[i];
  103.         chaine[i] = chaine[j];
  104.         chaine[i] = c;  
  105.     }
  106. }
  107.  
  108. /*
  109.  * %%Function: libQuote
  110.  *
  111.  * Protege le caractere "
  112.  */
  113. char * libQuote(char * chaine)
  114. {
  115.     register int i = 0, j = 0;
  116.  
  117.  
  118.     if(!chaine)
  119.         return chaine;
  120.  
  121.     if(strlen(chaine) > MLEN * 3)
  122.         return chaine;
  123.  
  124.     sprintf(libTmpTab, "%s", chaine);
  125.  
  126.     while(libTmpTab[i] != '\0')
  127.     {
  128.         if(libTmpTab[i] == '"')
  129.         {
  130.             for(j = strlen(libTmpTab); j >= i; j--)
  131.                 libTmpTab[j+1] = libTmpTab[j];
  132.  
  133.             libTmpTab[i++] = '\\';
  134.         }
  135.  
  136.         i++;
  137.     }
  138.  
  139.     return libTmpTab;
  140. }
  141.  
  142. /*
  143.  * %%Function: libDelQuote
  144.  *
  145.  * Supprime le caractere "
  146.  */
  147. char * libDelQuote(char * chaine)
  148. {
  149.     register int i = 0, j = 0;
  150.  
  151.  
  152.     if(!chaine)
  153.         return chaine;
  154.  
  155.     if(strlen(chaine) > MLEN * 3)
  156.         return chaine;
  157.  
  158.     sprintf(libTmpTab, "%s", chaine);
  159.  
  160.     while(libTmpTab[i] != '\0')
  161.     {
  162.         if(libTmpTab[i] == '"')
  163.         {
  164.             for(j = i; j < (int) strlen(libTmpTab); j++)
  165.                 libTmpTab[j] = libTmpTab[j+1];
  166.         }
  167.  
  168.         i++;
  169.     }
  170.  
  171.     return libTmpTab;
  172. }
  173.  
  174. /*
  175.  * %%Function: libChangeChar
  176.  *
  177.  * Supprime tous les espaces (blancs) situes en fin de chaine.
  178.  */
  179. char * libChangeChar(char * chaine, char car, char change)
  180. {
  181.     register int i = 0;
  182.     int len = strlen(chaine);
  183.  
  184.  
  185.     if(!chaine)
  186.         return chaine;
  187.  
  188.     if(len > MLEN * 3)
  189.         return chaine;
  190.  
  191.     sprintf(libTmpTab, "%s", chaine);
  192.  
  193.     while(i < len)
  194.         if(libTmpTab[i++] == car)
  195.             libTmpTab[i-1] = change;
  196.  
  197.     return libTmpTab;
  198. }
  199.  
  200. /*
  201.  * %%Function: libRTrim
  202.  *
  203.  * Supprime tous les espaces (blancs) situes en fin de chaine.
  204.  */
  205. char * libRTrim(char * chaine)
  206. {
  207.     if(!chaine)
  208.         return chaine;
  209.  
  210.     while(chaine[strlen(chaine)-1] == ' ' || chaine[strlen(chaine)-1] == '\t')
  211.         chaine[strlen(chaine)-1] = '\0';
  212.  
  213.     return chaine;
  214. }
  215.  
  216. /*
  217.  * %%Function: libLTrim
  218.  *
  219.  * Supprime tous les espaces (blancs) situes en debut de chaine.
  220.  */
  221. char * libLTrim(char * chaine)
  222. {
  223.     register int ct = 0;
  224.  
  225.  
  226.     if(!chaine)
  227.         return chaine;
  228.  
  229.     while(chaine[0] == ' ' || chaine[0] == '\t')
  230.     {
  231.         for(ct = 0; ct < (int)strlen(chaine); ct++)
  232.             chaine[ct] = chaine[ct+1];
  233.     }
  234.  
  235.     return chaine;
  236. }
  237.  
  238. /*
  239.  * %%Function: libCaseString
  240.  *
  241.  * Conversion d'une chaine de caracteres en majuscule ou en minuscule
  242.  * suivant l'option passee en argument. La chaine retournee est allouee
  243.  * en memoire.
  244.  */
  245. char * libCaseString(char * chaine, int option)
  246. {                              
  247.     char    * newChaine = (char *) 0;
  248.     int     ct = 0;
  249.               
  250.               
  251.     if (!chaine)
  252.         return chaine;
  253.     if (!(newChaine = (char *)calloc(strlen(chaine), (size_t)sizeof(char))))
  254.         return chaine;
  255.     
  256.     while(ct <= (int)strlen(chaine))
  257.     {
  258.         if(option == UPPERCASE)
  259.             newChaine[ct] = (char)toupper((int)chaine[ct++]);
  260.         else
  261.             newChaine[ct] = (char)tolower((int)chaine[ct++]);
  262.     }
  263.     
  264.     newChaine[ct] = '\0';
  265.     
  266.     return newChaine;
  267. }     
  268.  
  269.  
  270. /* ------------------------------------------------------------------------------- */
  271.  
  272.     
  273. /*
  274.  * %%Function: libPrintCharStd
  275.  *
  276.  * Send a character to the std file.
  277.  */
  278. void libPrintCharStd(int ch, FILE * std)
  279. {   
  280.     if(std)
  281.         putc(ch, std);
  282. }
  283.  
  284. /*
  285.  * %%Function: libPrintString
  286.  *
  287.  * Send a string to the std file.
  288.  */
  289. void libPrintString(char * string, FILE * std)
  290. {
  291.     if(string && std)
  292.     {
  293.         fflush(std);
  294.         fprintf(std, "%s", string);
  295.         fflush(std);
  296.     }
  297. }
  298.  
  299. /*
  300.  * %%Function: libPrintInt
  301.  *
  302.  * Send an integer to the std file.
  303.  */
  304. void libPrintInt(int integer, FILE * std)
  305. {
  306.     if(std)
  307.         fprintf(std, "%d", integer);
  308. }
  309.  
  310. /*
  311.  * %%Function: libPrintLong
  312.  *
  313.  * Send a Long integer to the std file.
  314.  */
  315. void libPrintLong(long integer, FILE * std)
  316. {
  317.     if(std)
  318.         fprintf(std, "%ld", integer);
  319. }
  320.  
  321. /*
  322.  * %%Function: libPrintStringInt
  323.  *
  324.  * Send a string and an integer to the std file.
  325.  */
  326. void libPrintStringInt(char * string, int integer, FILE * std)
  327. {
  328.     if(std)
  329.     {
  330.         fprintf(std, "%s %d", string, integer);
  331.         fflush(std);
  332.     }
  333. }
  334.  
  335. /*
  336.  * %%Function: libPosChar.
  337.  *
  338.  * Renvoie la premiere position d'un caractere dans une chaine,
  339.  * si le caractere n'est pas trouve la fonction renvoie -1.
  340.  *
  341.  * L'option (flag) permet de choisir de lancer la recherche par le
  342.  * debut de la chaine (true) ou par la fin (false).
  343.  */
  344. int libPosChar(char * chaine, char leChar, bool flag)
  345. {
  346.     int ct = 0, len = strlen(chaine);
  347.  
  348.     if(flag == false)
  349.     {
  350.         len = len - 1;
  351.         while(len != ct) if(chaine[len--] == leChar) return ++len;
  352.     }
  353.     else
  354.         while(ct < len) if(chaine[ct++] == leChar) return --ct;
  355.  
  356.     return -1;
  357. }
  358.  
  359.  
  360. /* ------------------------------------------------------------------------- */
  361.  
  362.  
  363. /*
  364.  * %%Function: libGetStrToChar.
  365.  *
  366.  * Renvoie une chaine de caracteres jusqu'au caractere donne
  367.  * ou par defaut la fin de ligne (ou fin de fichier).
  368.  */
  369. char * libGetStrToChar (FILE * fp, int caractere)
  370. {
  371.     register int ch = 0, ct = 0;
  372.  
  373.  
  374.     if(fp)
  375.     {
  376.         ch = getc(fp);
  377.  
  378.         while (ch != EOF && ch != '\n' && ch != caractere)
  379.         {
  380.             if(ct + 2 < MLEN * 4)
  381.             {
  382.                 libTmpTab[ct++] = (char)ch;
  383.                 libTmpTab[ct] = '\0';
  384.             }
  385.  
  386.             ch = getc(fp);
  387.         }
  388.     }
  389.  
  390.     oldChar = ch;
  391.  
  392.     return libTmpTab;
  393. }
  394.  
  395. /*
  396.  * %%Function: libGetStrToCR.
  397.  *
  398.  * Renvoie une chaine de caracteres jusqu'a la fin de ligne
  399.  * issue d'un fichier.
  400.  */
  401. char * libGetStrToCR (FILE * fp)
  402. {
  403.     return libGetStrToChar(fp, '\n');
  404. }
  405.  
  406. /*
  407.  * %%Function: libNextLine.
  408.  *
  409.  * On deplace le file pointeur jusqu'au debut de la ligne suivante.
  410.  */
  411. void libNextLine(FILE * fp)
  412. {
  413.     register int ch = 0;
  414.  
  415.  
  416.     if(fp)
  417.     {
  418.         ch = getc(fp);
  419.  
  420.         while (ch != EOF && ch != '\n')
  421.             ch = getc(fp);
  422.     }
  423.  
  424.     oldChar = ch;
  425. }
  426.  
  427. /*
  428.  * %%Function: libGotoChar.
  429.  *
  430.  * On deplace le file pointeur jusqu'au prochain caractere
  431.  * ou a defaut la fin de ligne (ou fin de fichier).
  432.  */
  433. void libGotoChar(FILE * fp, int caractere)
  434. {
  435.     register int ch = 0;
  436.  
  437.  
  438.     if(fp)
  439.     {
  440.         ch = getc(fp);
  441.  
  442.         while (ch != EOF && ch != '\n' && ch != caractere)
  443.             ch = getc(fp);
  444.     }
  445.  
  446.     oldChar = ch;
  447. }
  448.  
  449. /*
  450.  * %%Function: libTypeOldChar.
  451.  *
  452.  * Test l'egalite du dernier caractere traite par les fonctions
  453.  * lib en lecture sur un fichier par caractere.
  454.  */
  455. bool libTypeOldChar(int caractere)
  456. {
  457.     if(caractere == oldChar)
  458.         return true;
  459.     else
  460.         return false;
  461. }
  462.  
  463.